<html>
<head><meta charset="utf-8"><title>Expr type from TyCtxt? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html">Expr type from TyCtxt?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223610299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223610299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223610299">(Jan 22 2021 at 07:22)</a>:</h4>
<p>How do I get the type of an <code>Expr</code> from a <code>TyCtxt</code>? I see that there's a <code>typeOf</code> method, but it takes a <code>DefId</code>, and <code>Expr</code> has a <code>LocalDefId</code> as far as I can see. Is there a way to get a <code>DefId</code> from a <code>LocalDefId</code>?</p>



<a name="223610914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223610914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lqd <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223610914">(Jan 22 2021 at 07:34)</a>:</h4>
<p>wasn't there a <code>local_def_id.to_def_id()</code> ?</p>



<a name="223611180"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223611180" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223611180">(Jan 22 2021 at 07:38)</a>:</h4>
<p>That type checks, thanks</p>



<a name="223611425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223611425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223611425">(Jan 22 2021 at 07:43)</a>:</h4>
<p>Hmm, it seems like this line</p>
<div class="codehilite"><pre><span></span><code>            let ty = cx.tcx.type_of(expr.hir_id.owner.to_def_id());
</code></pre></div>
<p>Sometimes fails with</p>
<div class="codehilite"><pre><span></span><code>compute_type_of_item: unexpected item type: ...
</code></pre></div>
<p>It's the <code>rustc_typeck::collect::type_of::type_of</code> function. Is there a version of this that returns an <code>Option</code>, or some predicate for things that can be type checked?</p>



<a name="223612205"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223612205" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223612205">(Jan 22 2021 at 07:56)</a>:</h4>
<p>Hmm, so <code>hir_id.owner</code> gives me the <code>DefId</code> of the enclosing function, trait etc. it's not what I want. I'm not sure how to get <code>DefId</code> of an expression.</p>



<a name="223612459"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223612459" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> osa1 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223612459">(Jan 22 2021 at 08:00)</a>:</h4>
<p>Trying this now: <code>cx.tcx.type_of(cx.tcx.hir().local_def_id(expr.hir_id).to_def_id())</code></p>



<a name="223646806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223646806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223646806">(Jan 22 2021 at 14:21)</a>:</h4>
<p><span class="user-mention" data-user-id="119169">@osa1</span> I don't think expressions have a DefId</p>



<a name="223646811"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223646811" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223646811">(Jan 22 2021 at 14:21)</a>:</h4>
<p>they're not serialized cross-crate</p>



<a name="223646925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223646925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223646925">(Jan 22 2021 at 14:22)</a>:</h4>
<p><code>type_of</code> is only for items</p>



<a name="223647036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Expr%20type%20from%20TyCtxt%3F/near/223647036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Expr.20type.20from.20TyCtxt.3F.html#223647036">(Jan 22 2021 at 14:23)</a>:</h4>
<p>The type of expressions is stored in the <code>TypeckResults</code> of the containing item</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>